<?php

namespace Seguridad\Model\Dao;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Seguridad\Model\Dao\GenericDAO;

class BitacoraDAO extends GenericDAO {

    protected $dbAdapter;

    /**
     * Nombre de la Tabla
     * @var type string
     */
    protected $tabla = 'bitacora';
    private static $bitacoraDao;

    /**
     * Configura Adaptador de Base de Datos
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    private function __construct(Adapter $adapter) {
        $this->dbAdapter = $adapter;
    }

    /**
     * 
     * @param type $adapter
     * @return instancia de BitacoraDao
     * Patron singleton
     */
    public static function getInstance($adapter) {
        if (self::$bitacoraDao == null) {
            self::$bitacoraDao = new BitacoraDao($adapter);
        }
        return self::$bitacoraDao;
    }

    public function getRegistro($id) {
        return $this->getRegistroGeneral($id, $this->tabla, $this->dbAdapter);
    }

    /**
     * Consulta que retorna la cantidad deregistros de la tabla bitacora
     * @param 
     * @return type resultadosBaseDeDatos
     * SQL SELECT COUNT(*) AS "num" FROM "bitacora"
     */
    public function contarRegistros() {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->tabla);
        $select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)')));
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }

    /**
     * @param type $data
     * @return type
     */
    public function createRegistro($data) {
        $columns = array();
        $values = array();
        foreach ($data as $column => $value) {
            $columns[] = $column;
            $values[$column] = (string) $value;
        }echo $sql;
        $sql = new Sql($this->dbAdapter);
        
        $insert = $sql->insert();
        $insert->into($this->tabla);
        $insert->columns($columns);
        $insert->values($values);
        $statement = $sql->prepareStatementForSqlObject($insert);
        if ($statement->execute()) {
            return true;
        } else {
            return false;
        }
    }

}
